package com.lan.schedule;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * Created by Administrator on 2020/3/14.
 */

public class MyLinearView extends TextView {

    Paint paint=null;
    LinearGradient linearGradient=null;
    Matrix matrix=null;
    float cureentgriendSize=0 ;  //等于2个文字大小
    public MyLinearView(Context context) {
        super(context);
    }

    public MyLinearView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }
    // 试图显示的时候，调用一次
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 这里必须要getPaint
        paint = getPaint();
        //GradientSize=两个文字的大小
        String text = getText().toString();
        float textWidth = paint.measureText(text);
        int GradientSize =(int) (3*textWidth/text.length());
        // 开始位置向左偏移3个位置渐变
        linearGradient = new LinearGradient(-GradientSize, 0, 0, 0, new int[]{0x22ffffff,0xffffffff,0x22ffffff}, new float[]{0,0.5f,1}, Shader.TileMode.CLAMP);//边缘融合
        paint.setShader(linearGradient);
        matrix = new Matrix();
        //    postInvalidate();
    }
    private float translateX;
    private float deltaX = 20;

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        float textWidth = getPaint().measureText(getText().toString());
        translateX += deltaX;  //20
        if(translateX > textWidth + 1|| translateX < 1){
            deltaX = -deltaX;
        }
//		matrix.setScale(sx, sy)
        // 设置 渲染切换
        matrix.setTranslate(translateX, 0);
        linearGradient.setLocalMatrix(matrix);

        postInvalidateDelayed(50);


    }
}